package edu.xidian.bdilab.jrbigdata.service.admin.bankadmin.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.xidian.bdilab.jrbigdata.controller.bank.BankCode;
import edu.xidian.bdilab.jrbigdata.controller.response.ResponseResult;
import edu.xidian.bdilab.jrbigdata.domain.BankInfo;
import edu.xidian.bdilab.jrbigdata.domain.BankUser;
import edu.xidian.bdilab.jrbigdata.domain.FinancialProduct;
import edu.xidian.bdilab.jrbigdata.domain.ProductRequest;
import edu.xidian.bdilab.jrbigdata.mapper.FinancialProductMapper;
import edu.xidian.bdilab.jrbigdata.mapper.ProductRequestMapper;
import edu.xidian.bdilab.jrbigdata.service.admin.bankadmin.bankManageService;
import org.springframework.beans.factory.annotation.Autowired;
import edu.xidian.bdilab.jrbigdata.mapper.BankInfoMapper;
import edu.xidian.bdilab.jrbigdata.mapper.BankUserMapper;
import org.springframework.stereotype.Service;
import edu.xidian.bdilab.jrbigdata.controller.admin.AdminCode;

import java.util.List;
@Service
public class bankManageServiceimpl implements  bankManageService{
    @Autowired
    BankInfoMapper bankInfoMapper;
    @Autowired
    BankUserMapper bankUserMapper;
    @Autowired
    ProductRequestMapper productRequestMapper;
    @Autowired
    FinancialProductMapper financialProductMapper;

    @Override
    public ResponseResult bankManage_check() throws Exception
    {
        ResponseResult responseResult=new ResponseResult();
        try{
            List<BankInfo> banks=bankInfoMapper.selectList(null);
            if(banks.size()!=0)
            {
                responseResult.setData(banks);
                responseResult.setSuccess(Boolean.TRUE);
                responseResult.setCode(AdminCode.SelectSUCCESS.getCode());
                responseResult.setMessage(AdminCode.SelectSUCCESS.getMessage());
                return responseResult;
            }
            else
            {
                responseResult.setSuccess(true);
                responseResult.setCode(BankCode.CHECK_NONE.getCode());
                responseResult.setMessage(BankCode.CHECK_NONE.getMessage());
            }
        }catch (Exception e){
            responseResult.setSuccess(Boolean.FALSE);
            responseResult.setCode(AdminCode.SelectFailed.getCode());
            responseResult.setMessage(AdminCode.SelectFailed.getMessage());
            return responseResult;
        }
       return responseResult;

    }

    @Override
    public ResponseResult  bankManage_delete(String userName) throws Exception
    {
        ResponseResult responseResult=new ResponseResult();
        QueryWrapper<BankInfo> bankInfoQueryWrapper=new QueryWrapper<>();
        bankInfoQueryWrapper.eq(userName!=null,"fk_user_name",userName);
        try{
            //删除该银行旗下的所有金融产品被申请的记录
            BankInfo bankInfo=bankInfoMapper.selectOne(bankInfoQueryWrapper);
            if(bankInfo!=null){
                int bankId=bankInfo.getBankId();
                //删除该银行旗下的所有金融产品
                QueryWrapper<FinancialProduct> financialProductQueryWrapper=new QueryWrapper<>();
                financialProductQueryWrapper.eq("fk_bank_id",bankId);
                List<FinancialProduct> financialProductList=financialProductMapper.selectList(financialProductQueryWrapper);
                if(financialProductList.size()!=0){
                    for(FinancialProduct financialProduct:financialProductList){
                        QueryWrapper<ProductRequest> productRequestQueryWrapper=new QueryWrapper<>();
                        productRequestQueryWrapper.eq("fk_product_name",financialProduct.getProductName());
                        productRequestMapper.delete(productRequestQueryWrapper);
                    }
                }
                financialProductMapper.delete(financialProductQueryWrapper);

            }
            //删除该银行用户
            int a=bankInfoMapper.delete(bankInfoQueryWrapper);
            if(a!=0){
                QueryWrapper<BankUser> bankUserQueryWrapper=new QueryWrapper<>();
                bankUserQueryWrapper.eq(userName!=null,"user_name",userName);
                int b=bankUserMapper.delete(bankUserQueryWrapper);
                if(b!=0){
                    responseResult.setSuccess(Boolean.TRUE);
                    responseResult.setCode(AdminCode.DELEATESUCCESS.getCode());
                    responseResult.setMessage(AdminCode.DELEATESUCCESS.getMessage());
                    return responseResult;
                }
            }
            responseResult.setSuccess(Boolean.FALSE);
            responseResult.setCode(AdminCode.DELEATEFAILED.getCode());
            responseResult.setMessage(AdminCode.DELEATEFAILED.getMessage());
            return responseResult;
        }catch (Exception e){
            responseResult.setSuccess(Boolean.FALSE);
            responseResult.setCode(AdminCode.DELEATEFAILED.getCode());
            responseResult.setMessage(AdminCode.DELEATEFAILED.getMessage());
            return responseResult;
        }
    }

}
